<table class="form-table-without-line">
    <tr>
        <th>期号</th>
        <td>
            <input type="text" class="am-form-field issue" placeholder="输入待设置的期数" />
            <button type="button" class="see">查看</button>
        </td>
    </tr>
    <tr>
        <th>开奖结果</th>
        <td>
            <input type="text" class="am-form-field result" placeholder="8个开奖号码，请用,隔开" />
            <button type="button" class="save">保存</button>
        </td>
    </tr>
    
    <tr>
        <th>杀率模式</th>
        <td>
            <select class="mode">
                <option value="-1">无</option>
                <option value="1">针对所有用户</option>
                <option value="2">针对部分用户</option>
            </select>
            <button type="button" class="mode-save">保存</button>
        </td>
    </tr>
    
    <tr>
        <th>全局杀率</th>
        <td>
            <input type="number" class="am-form-field global-rate" placeholder="例如：0.8表示80%概率杀" />
            <button type="button" class="global-rate-save">保存</button>
        </td>
    </tr>
    
    <tr>
        <th>用户杀率</th>
        <td>
            <input type="number" class="am-form-field user-rate" placeholder="例如：0.8表示80%概率杀" />
            <textarea class="user-list" rows="10" placeholder="请输入起效的用户列表，用,分割"></textarea>
            <button type="button" class="user-rate-save">保存</button>
        </td>
    </tr>
</table>

<script src="../public/all/js/result.js"></script>
<script>
    // 将最近一期的期号和开奖时间填入
    var game = 'zykl10';
    Result.switchGame(game);
    Result.cb = function() {
        var curr = Result.curr();
        if (!curr) return;

        $(".issue").val(curr.issue);
        updateIssueResult(curr.issue);
    };

    // 更新某一期的设置结果
    function updateIssueResult(issue) {
        CMD.query_zy_lottery_result(function(r) {
            for (var i in r.list) {
                if (r.list[i].issue == issue) {
                    // 数据填入
                    fill_results(r.list[i].red.split(","));
                }
            }
        });
    }

    $(".see").click(function() {
        var issue = $(".issue").val().trim();
        if (issue == '') return;

        updateIssueResult(issue);
    });

    function fill_results(list) {
        $("input.result").val(list.join(","));
    }

    $("button.save").click(function() {
        var arr = $("input.result").val().trim().split(",");
        if (arr.length != 8) {
            alert2("开奖号码应该为8个！", true);
            return;
        }

        for (var i = 0; i < arr.length; i++) {
            var n = arr[i];
            if (n == '' || parseInt(n) < 1 || parseInt(n) > 20) {
                alert2("开奖结果数字应该为1-20。", true);
                return;
            }
            n = parseInt(n);
            arr[i] = n;

            // 确保数字不重复
            for (var j = 0; j < i; j++) {
                if (arr[j] == arr[i]) {
                    alert2("开奖数字不能重复！", true);
                    return;
                }
            }
        } 

        // 保存
        startLoading();
        CMD.set_zy_lottery_result(game, $(".issue").val().trim(), arr, function() {
            stopLoading();
            alert2("设置成功！");
        });
    });

    // 杀率的设置
    lottery_ai(game);
</script>
